@ECHO OFF

REM **********************************************************************************************/
REM Install.cmd
REM
REM Dependencies:
REM The SQL Server utility osql must be located on the computer's path
REM
REM This is currently for the Latest Version install for the Client DB system.
REM This should be wiped and started fresh with each new version.
REM Put the current version that this file is for in this header section and the ProdjectName local var below...
REM
REM ********************************************************************
REM --------------------------------------------------------------------
REM						WEB VERSION SETUP
REM --------------------------------------------------------------------
REM ********************************************************************
REM
REM Date	 Author			Description
REM ----------	 -------------------	-----------------------------------------------------------------------
REM June 03 2012 Md. Hassanur Rahman    Initial script. (taken from the script that Shabbir Darugar (Microsoft) created.
REM **********************************************************************************************/

REM -- Setting Variables
SET StartTime=%TIME%
SET ProjectName=BDMarket Database Setup
SET CMDFile=Install
SET ExitCode=0
SET SilentInstall=0

REM -- Checking Parameters
IF "%~1" == "" (GOTO BadParam) ELSE (SET Server=%~1)
IF "%~2" == "" (GOTO BadParam) ELSE (SET Database=%~2)
IF "%~3" == "" (GOTO BadParam) ELSE (SET uid=%~3)
IF "%~4" == "" (GOTO BadParam) ELSE (SET pwd=%~4)
IF "%~5" == "" (SET LogFile=%CMDFile%.log) ELSE (SET LogFile=%~5)
IF "%~6" == "-silent" (SET SilentInstall=1)

ECHO.
ECHO Parameters Passed in:
ECHO Server: %Server%
ECHO Database: %Database%
ECHO UID: %uid%
ECHO PWD: %pwd%
ECHO LogFile: %LogFile%
ECHO SilentInatall: %SilentInstall%
ECHO.


REM -- Displaying message on screen
ECHO *******************************************************************************
ECHO Executing %ProjectName% scripts on \\%Server%\%Database%
ECHO on %DATE% at %StartTime%.
ECHO *******************************************************************************


REM -- Writing message to Log File
ECHO ******************************************************************************* > %LogFile%
ECHO Executing %ProjectName% on \\%Server%\%Database% >> %LogFile%
ECHO at %DATE% %StartTime%. >> %LogFile%
ECHO ******************************************************************************* >> %LogFile%


ECHO Connecting to \\%Server%\%Database%...
ECHO ******************************************************************************* >> %LogFile%
ECHO Connecting to \\%Server%\%Database%... >> %LogFile%
ECHO. >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%
CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -n -b -Q "PRINT 'Connection Successful'" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)


ECHO Creating Tables...
ECHO. >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%
ECHO Creating Tables >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%


CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\SavedState.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Religion.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Country.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Category.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Configuration.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\NewsLetter.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Manufacturer.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\IndustryType.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Group.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\EmailMessage.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\ProductIndex.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)


CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\UOM.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\User.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\District.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Content.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Company.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\CompanyLogo.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Product.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Contact.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\UserWishList.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\UserContact.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\ProductImage.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Store.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\Transaction.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\TransactionDetail.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\ProductVisitDetails.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Tables\StoreLedger.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)



ECHO Creating Functions...
ECHO. >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%
ECHO Creating Functions >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Functions\SplitString.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)



ECHO Creating Stored Procedures...
ECHO. >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%
ECHO Creating Stored Procedures >> %LogFile%
ECHO -------------------------------------------------------------------------------- >> %LogFile%

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetContactsByCompany.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetContentList.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductByDiscount.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductByPopularSearch.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductBySearch.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductBySpecialOffers.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductsByCategory.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductsByCompany.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedRandomProduct.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetProductsForReIndex.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetUsersByCompany.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductByPopularSearch.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetTopViewedProducts.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetRecentProducts.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetCategoryList.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetPagedProductsFromWishList.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetProductsByID.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)

CALL osql -w 125 -S %Server% -d %Database% -U%uid% -P%pwd% -l30 -n -b -i "..\Stored Procedures\GetLookupData.sql" >> %LogFile%
IF %ERRORLEVEL% NEQ 0 (GOTO Crash)




REM -- Displaying message on screen
ECHO *******************************************************************************
ECHO %ProjectName% scripts executed successfully on \\%Server%\%Database%
ECHO Started on: %DATE% at %starttime%
ECHO Finished on: %DATE% at %TIME%
ECHO.
ECHO Please check the log file %LogFile% for details.
ECHO *******************************************************************************


REM -- Writing message to Log File
ECHO. >> %LogFile%
ECHO ******************************************************************************* >> %LogFile%
ECHO %ProjectName% scripts executed successfully on \\%Server%\%Database% >> %LogFile%
ECHO Started on: %DATE% at %starttime% >> %LogFile%
ECHO Finished on: %DATE% at %TIME% >> %LogFile%
ECHO ******************************************************************************* >> %LogFile%

GOTO End


REM -- Subroutine: BadParam --------------------------------------------------------
:BadParam
ECHO *******************************************************************************
ECHO Please enter valid parameters to execute these scripts.
ECHO.
ECHO %CMDFile% (Server) (Database) [(LogFile)] [-silent]
ECHO EXAMPLE %CMDFile% TestServer TestDB TestDB.log
ECHO EXAMPLE %CMDFile% TestServer TestDB
ECHO *******************************************************************************

SET ExitCode=1
GOTO End
REM --------------------------------------------------------------------------------


REM -- Subroutine: Crash -----------------------------------------------------------
:Crash
ECHO *******************************************************************************
ECHO Failed to execute %ProjectName% scripts on \\%Server%\%Database%
ECHO Started on: %DATE% at %starttime%
ECHO Finished on: %DATE% at %TIME%
ECHO.
ECHO Please check the log file %LogFile% for Errors!
ECHO *******************************************************************************

SET ExitCode=1
GOTO End
REM --------------------------------------------------------------------------------

REM -- Subroutine: End -------------------------------------------------------------
:End
ECHO.
ECHO.
IF %SilentInstall% == 1 (EXIT %ExitCode%) ELSE (pause)
REM --------------------------------------------------------------------------------
